<!-- 补报(公办) -->
<template>
	<zh-app style="background-color: white;font-family: '阿里巴巴普惠体'">
		<!-- 导航栏 -->
		<zh-navbar title="公办补报" color="#85a5ff" linear="right"  />
		<zh-filterMenu >
			<!-- 年度 -->
			<zh-filterMenu-item isButton  :title="data.year+'年'" un-icon="" icon="tmicon-menu">
				
			</zh-filterMenu-item>
			<!-- 统筹区 -->
			<zh-filterMenu-item :title="data.areaName">
				<view hover-class="opactiy-7" v-for="(item,index) in data.areaCodeDict" :key="index"
					@tap="onSelectArea(item)"
					class="flex-1 flex-row flex flex-row-center-between" style="height: 76rpx;">
					<zh-text :color="item.dictKey===data.areaCode?'primary':''" :label="item.dictValue" />
					<tm-icon :font-size="24" color="primary" v-if="item.dictKey===data.areaCode" name="tmicon-check" />
				</view>	
			</zh-filterMenu-item>
		</zh-filterMenu>
		<view v-if="!data.showFlag" >
			<zh-result  status="lock" color="amber" :showBtn="false" :title="data.operation.title" :subTitle="data.operation.subTitle"></zh-result>
			<view class="flex-row-center-center">
			<zh-button  label="返回" @click="goBack"  linear="right"></zh-button>
			</view>
		</view>
		<view  v-if="data.showFlag">
			<zh-sheet :padding="[10,20]"  text :color="data.fullName?'primary':'deep-orange'" @click="onChangeStu">
				<view class="flex-row-center-start">
					<zh-text  v-if="data.fullName"  label="报名学生:"></zh-text>
					<zh-text  v-if="data.fullName"  class="text-weight-b" :label="data.fullName"></zh-text>
				</view>
				<zh-text  v-if="!data.fullName" label="请选择报名学生"></zh-text>
			</zh-sheet>
			<zh-sheet v-if="data.fullName" :padding="[0,0]">
				<zh-input
					:height="80" 
					v-model="data.searchText"
					placeholder="可输入学校名称搜索" 
					placeholderStyle="text-align: center;" 
					 :transprent="false"
					showClear
				/>
			</zh-sheet>
			<!-- 筛选结果列表 -->
			<z-paging
				v-if="data.fullName"
				ref="paging"
				:fixed="false"
				height="75vh"
				@query="onQueryList"
				v-model="data.schoolList"
				:show-scrollbar="false"
			>
				<template v-slot:empty>
					<zh-result
						:showBtn="false"
						title="未搜索到补报学校信息"
						subTitle="请重新输入名称搜索"
					></zh-result>
				</template>
				<z-paging-cell
					v-for="(item, index) in data.schoolList"
					:key="item.schoolId"
				>
					<schoolInfo :school="item" :studentEnrollPreId="data.studentEnrollPreId">	</schoolInfo>
				</z-paging-cell>
				
			</z-paging>
		</view>
		<!-- 底部学生选择栏 -->
		<zh-action-menu rangKey="fullName"  v-model="data.showStudent" :list="data.stuArr" @change="onCheckStu"></zh-action-menu>
		<zh-message ref="messageRef" :duration="2000"></zh-message>
	</zh-app>
</template>
<script lang="ts" setup>
	import {ref, reactive} from "vue";
	import { onLoad,onShow } from "@dcloudio/uni-app";
	import schoolInfo from './components/schoolInfo.vue';
	// ------------------接收传入参数(组件)-------------------

	// ------------------变量区------------------------------
	const messageRef=ref()
	const paging=ref();
	// 主数据
	const data = reactive({
		showFlag:false,//是否显示报名信息
		showStudent:false,
		year:new Date().getFullYear(),
		areaCode:'',
		areaName:'报名区县',
		operation:{title:'不能办理该业务',subTitle:'本年度招生工作尚未开始'},
		areaCodeDict:[] as any,
		searchText:'',//学校搜索
		schoolList:[] as any,//可报名学校列表
		studentEnrollPreId:'',//将要报名的学生
		fullName:'',//学生姓名
		schoolCategory:'',//选择学生的学段
		// 学生
		stuArr: [] as any,
		firstFlag:true,
		canSearch:false,
	})

	// ------------------方法区------------------------------
	//获取可选行政区划
	const getAreaCode=()=>{
	  $api.common.send.post("dict0028", {}).then((res: any) => {
			data.areaCodeDict = res.result||[];
			if(data.areaCodeDict.length>=1){
			  data.areaCode=data.areaCodeDict[0].dictKey
			  data.areaName=data.areaCodeDict[0].dictValue
			  checkBm()
			}
		}).catch(()=>{})
	}
	//选择行政区划
	const onSelectArea=async (item:any)=>{
		data.areaCode=item.dictKey
		data.areaName=item.dictValue
		checkBm()
	}
	//获取年度
	const getYear=()=>{
		$api.common.send.post("OperationCache0005", {}).then((res: any) => {
			data.year=res.result.year||new Date().getFullYear()
		}).catch(()=>{})
	}
	// 报名前信息检查
	const checkBm = async () => {
			//判断是否学生
			let studentRet=await $api.common.send.post("eduStudentEnroll0007",{})
			if(studentRet.result){
				data.operation.title='学生账号不能报名！'
				data.operation.subTitle='请登录家长浙里办账号进行报名！'
				data.showFlag=false
				return
			}
			//判断是否在业务期范围内
			const params={classify:'2',areaCode:data.areaCode}
			$api.common.send.post("OperationCache0001", params).then((res: any) => {
				let result=res.result
				if(result&&result.operation){
					let eduOperation :any=result.operation
					let YW022 :any={}//补报 第二轮
					let YW010 :any={}//抢报 第三轮
					if(eduOperation){
						YW022=eduOperation.YW022
						YW010=eduOperation.YW010
						if(YW022.flag||YW010.flag){
							//报名中获取报名列表
							getStus()
						}else{
							
								if(YW022.type==='1'){
									data.operation.title='未在办理补报业务时间段内！'
									data.operation.subTitle=YW022.messagePrompts
									data.showFlag=false
									return
								}else if(YW022.type==='3'){
									data.operation.title='未在办理补报业务时间段内！'
									data.operation.subTitle=data.year+'年度义务教育段补报工作尚未开始！'
									data.showFlag=false
									return
								}else{
									if(YW010.type==='1'){
										data.operation.title='未在办理补报业务时间段内！'
										data.operation.subTitle=YW010.messagePrompts
										data.showFlag=false
										return
									}else if(YW010.type==='2'){
										data.operation.title='未在办理补报业务时间段内！'
										data.operation.subTitle=YW010.messageTip
										data.showFlag=false
										return
									}else{
										data.operation.title='未在办理补报业务时间段内！'
										data.operation.subTitle=YW022.messageTip
										data.showFlag=false
										return
									}
								}
							
						}
					}else{
						data.operation.title='未在办理补报业务时间段内！'
						data.operation.subTitle=data.year+'年度义务教育段补报工作尚未开始！'
						data.showFlag=false
						return
					}
					
				}else{
					data.operation.title='未在办理补报业务时间段内！'
					data.operation.subTitle=data.year+'年度义务教育段补报工作尚未开始！'
					data.showFlag=false
					return
				}
			}).catch(()=>{})
		
	}
	const getStus = () => {
		//获取可补报的学生信息
		const params={}
		$api.common.send.post("eduStudentEnroll0023", params).then((res: any) => {
			data.stuArr=res.result||[]
			data.firstFlag=false
			if(data.stuArr.length>1){
				data.showStudent=true
				data.showFlag=true
				
			}else if(data.stuArr.length===1){
				data.schoolCategory=data.stuArr[0].schoolCategory
				data.fullName=data.stuArr[0].fullName
				data.studentEnrollPreId=data.stuArr[0].studentEnrollPreId
				data.showFlag=true
			}else{
				data.operation.title='没有需要报名的学生！'
				data.operation.subTitle='请前往当年适龄子女查看！'
				data.showFlag=false
			}
		}).catch((err:any)=>{
			if(err.code===2){
				//前往我的报名
				data.operation.title='没有需要报名的学生！'
				data.operation.subTitle='已报名,请前往当年适龄子女查看！'
				data.showFlag=false
			}else if(err.code===3){
				data.operation.title='没有可报名的学生！'
				data.operation.subTitle='无当年适龄子女！'
				data.showFlag=false
			}
		})
		
		
	}
	//人员选择
	const onChangeStu=()=>{
		if(data.stuArr.length>1){
			data.showFlag=true
		}
	}
	//人员选择后查询
	function onCheckStu(item: any) {
		data.schoolCategory=item.schoolCategory
		data.fullName=item.fullName
		data.studentEnrollPreId=item.studentEnrollPreId
		uni.$tm.u.debounce(() => paging.value?.reload(), 500)
		
	}
	const onQueryList = (pageNo :Number, pageSize :Number) => {
		//获取学校列表
		let params :any={}
		params.schoolName=data.searchText
		params.pageNum = pageNo
		params.pageSize = pageSize
		if(data.schoolCategory){
			params.schoolCategory=data.schoolCategory
		}else{
			params.schoolCategoryList=['2','3']
		}
		params.schoolNature='0'
		$api.common.send.post("eduschool0011",params).then((res:any)=>{
			paging.value?.complete(res.result.list || [])
		}).catch(() => {
			paging.value?.complete(false)
		})
	}
	const goBack=()=>{
		$router.back(1)
	}
	// ------------------钩子函数----------------------------
	// 初始化
	onLoad((options:any) => {
		if(options.areaCode){
		  data.areaCode=options.areaCode
		}
		getAreaCode()
		getYear()
	})
	
	onShow(() => {
		if(!data.firstFlag){
	    	checkBm()
		}
	});
	//名称搜索输入
	watch(
	  () => data.searchText,
	  () => {
	    data.canSearch=true
		onSearch()
	  },
	  { deep: true }
	);
	//关闭筛选器的时候触发搜索
	function onSearch() {
		if(data.canSearch){
			uni.$tm.u.debounce(() => paging.value?.reload(), 500)
		}
	}
</script>
<style lang="scss" scoped></style>